package com.kuangqi.paymentdemo.controller;

import com.kuangqi.paymentdemo.service.WeChatpayService;
import com.kuangqi.paymentdemo.service.impl.CacheService;
import com.kuangqi.paymentdemo.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author kuanggong
 */
@RestController
@RequestMapping("/api/wechatpay")
@Api(tags = "微信支付")
@CrossOrigin
@Slf4j
public class WeChatpayController {

    @Autowired
    WeChatpayService weChatpayService;
    @Autowired
    CacheService cacheService;

    @ApiOperation("nativePay")
    @PostMapping("native/{productId}")
        public R nativePay(@PathVariable Long productId) {
        //返回支付二维码链接和订单号
        Map<String, Object> map = weChatpayService.nativePay(productId);
        return R.ok().setData(map);
    }

    @ApiOperation("jsapi预下单")
    @PostMapping("jsapi")
    public R jsApi(@RequestBody Map<String, Object> payParam) {

        String result;
        if (cacheService.exists("prepay_id")) {
            result = cacheService.get("prepay_id");
        } else {
            result = weChatpayService.jsapiPay(payParam);
        }
        Map<String, Object> resultMap = new HashMap<>(1);
        resultMap.put("result", result);
        return R.ok().setData(resultMap);
    }

    @ApiOperation("微信支付关闭订单单功能,搭配延迟队列使用")
    @PostMapping("cancel/{orderno}")
    public R cancel(@PathVariable(value = "orderno") String orderNo) {
        log.info("取消订单");
        weChatpayService.cancelOrder(orderNo);
        return R.ok().setMessage("取消订单成功");
    }


    @ApiOperation("微信支付查单功能,搭配定时任务使用")
    @GetMapping("/query/{orderno}")
    public R queryOrder(@PathVariable(value = "orderno") String orderNo) {
        String status = weChatpayService.queryOrder(orderNo);
        log.info("查询订单");
        return R.ok().setMessage("订单状态:" + status);
    }

    @ApiOperation("申请退款")
    @GetMapping("/refunds/{orderno}/{reason}")
    public R refunds(@PathVariable(value = "orderno") String orderNo, @PathVariable String reason) {
        log.info("申请退款");
        weChatpayService.refund(orderNo, reason);
        return R.ok();
    }


    @ApiOperation("查询退款")
    @GetMapping("/query-refund/{refundno}")
    public R queryRefund(@PathVariable(value = "refundno") String refundNo) {

        log.info("查询退款");

        String result = weChatpayService.queryRefund(refundNo);
        return R.ok().setMessage("查询成功").data("result", result);
    }

    @ApiOperation("获取账单url")
    @GetMapping("/querybill/{billdate}/{type}")
    public R queryTradeBill(
            @PathVariable(value = "billdate") String billDate,
            @PathVariable String type)  {
        log.info("获取账单url");

        String downloadUrl = weChatpayService.queryBill(billDate, type);
        return R.ok().setMessage("获取账单url成功").data("downloadUrl", downloadUrl);
    }
    @ApiOperation("下载账单")
    @GetMapping("/downloadbill/{billdate}/{type}")
    @SneakyThrows
    public R downloadBill(
            @PathVariable(value = "billdate") String billDate,
            @PathVariable String type) {
        log.info("下载账单");
        String result = weChatpayService.downloadBill(billDate, type);
        return R.ok().data("result", result);
    }


}
